package com.br.sae.model.dao;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Query;

import com.br.sae.conexaobd.SistemaDAO;
import com.br.sae.model.entitys.Aluno;
import com.br.sae.model.entitys.Matricula;

public class MatriculaBD {

	public List<Matricula> consultaMatricula(String nivel) {
		String sql = "";
		sql = "SELECT matricula FROM Matricula matricula WHERE matricula.nivel= :nivel";

		Query query = SistemaDAO.createQuery(sql);
		query.setParameter("nivel", nivel);

		return query.getResultList();
	}

	public List<Matricula> consultaMatricula(String nome, String nivel) {
		String sql = "SELECT aluno FROM Aluno aluno WHERE aluno.nome= :nome";
		Query query = SistemaDAO.createQuery(sql);
		query.setParameter("nome", nome);
		List<Aluno> alunos = query.getResultList();

		String sqlMa = "";
		sqlMa = "SELECT matricula FROM Matricula matricula WHERE matricula.nivel= :nivel";

		Query queryMa = SistemaDAO.createQuery(sqlMa);
		queryMa.setParameter("nivel", nivel);

		List<Matricula> matriculas = queryMa.getResultList();

		List<Matricula> result = new ArrayList<Matricula>();

		for (Matricula ma : matriculas) {
			for (Aluno al : alunos) {
				if (ma.getAluno().getId() == al.getId()) {
					result.add(ma);
				}
			}
		}

		return result;
	}

	public boolean incluirMatricula(Matricula matricula) {
		try {
			SistemaDAO.persistMatricula(matricula);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean alterarMatricula(Matricula matricula) {
		try {
			SistemaDAO.updateMatricula(matricula);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean removerMatricula(Matricula matricula) {
		try {
			SistemaDAO.removerMatricula(matricula);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
}
